package com.zenithmind.exam.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zenithmind.exam.pojo.entity.WrongQuestion;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.Delete;

import java.util.List;

/**
 * 错题本Mapper接口
 * 
 * @author ZenithMind
 * @since 2024-07-01
 */
@Mapper
public interface WrongQuestionMapper extends BaseMapper<WrongQuestion> {

    /**
     * 根据用户ID查询错题列表
     * 
     * @param userId 用户ID
     * @return 错题列表
     */
    @Select("SELECT * FROM exam_wrong_question WHERE user_id = #{userId} AND is_mastered = 0 ORDER BY last_wrong_time DESC")
    List<WrongQuestion> selectByUserId(@Param("userId") String userId);

    /**
     * 根据用户ID查询已掌握的错题列表
     * 
     * @param userId 用户ID
     * @return 已掌握的错题列表
     */
    @Select("SELECT * FROM exam_wrong_question WHERE user_id = #{userId} AND is_mastered = 1 ORDER BY master_time DESC")
    List<WrongQuestion> selectMasteredByUserId(@Param("userId") String userId);

    /**
     * 根据题目ID查询错题记录列表
     * 
     * @param questionId 题目ID
     * @return 错题记录列表
     */
    @Select("SELECT * FROM exam_wrong_question WHERE question_id = #{questionId} ORDER BY last_wrong_time DESC")
    List<WrongQuestion> selectByQuestionId(@Param("questionId") String questionId);

    /**
     * 根据用户ID和题目ID查询错题记录
     * 
     * @param userId 用户ID
     * @param questionId 题目ID
     * @return 错题记录
     */
    @Select("SELECT * FROM exam_wrong_question WHERE user_id = #{userId} AND question_id = #{questionId}")
    WrongQuestion selectByUserIdAndQuestionId(@Param("userId") String userId, @Param("questionId") String questionId);

    /**
     * 分页查询用户错题
     * 
     * @param page 分页参数
     * @param userId 用户ID
     * @param isMastered 是否已掌握
     * @return 分页结果
     */
    IPage<WrongQuestion> selectWrongQuestionPage(Page<WrongQuestion> page,
                                                @Param("userId") String userId,
                                                @Param("isMastered") Integer isMastered);

    /**
     * 统计用户错题数量
     * 
     * @param userId 用户ID
     * @param isMastered 是否已掌握
     * @return 错题数量
     */
    @Select("SELECT COUNT(*) FROM exam_wrong_question WHERE user_id = #{userId} AND is_mastered = #{isMastered}")
    Integer countByUserIdAndMastered(@Param("userId") String userId, @Param("isMastered") Integer isMastered);

    /**
     * 统计题目错误次数
     * 
     * @param questionId 题目ID
     * @return 错误次数
     */
    @Select("SELECT SUM(wrong_count) FROM exam_wrong_question WHERE question_id = #{questionId}")
    Integer sumWrongCountByQuestionId(@Param("questionId") String questionId);

    /**
     * 更新错误次数
     * 
     * @param userId 用户ID
     * @param questionId 题目ID
     * @return 更新行数
     */
    @Update("UPDATE exam_wrong_question SET wrong_count = wrong_count + 1, last_wrong_time = NOW(), update_time = NOW() " +
            "WHERE user_id = #{userId} AND question_id = #{questionId}")
    int incrementWrongCount(@Param("userId") String userId, @Param("questionId") String questionId);

    /**
     * 标记题目为已掌握
     * 
     * @param userId 用户ID
     * @param questionId 题目ID
     * @return 更新行数
     */
    @Update("UPDATE exam_wrong_question SET is_mastered = 1, master_time = NOW(), update_time = NOW() " +
            "WHERE user_id = #{userId} AND question_id = #{questionId}")
    int markAsMastered(@Param("userId") String userId, @Param("questionId") String questionId);

    /**
     * 标记题目为未掌握
     * 
     * @param userId 用户ID
     * @param questionId 题目ID
     * @return 更新行数
     */
    @Update("UPDATE exam_wrong_question SET is_mastered = 0, master_time = NULL, update_time = NOW() " +
            "WHERE user_id = #{userId} AND question_id = #{questionId}")
    int markAsNotMastered(@Param("userId") String userId, @Param("questionId") String questionId);

    /**
     * 根据用户ID删除所有错题记录
     * 
     * @param userId 用户ID
     * @return 删除行数
     */
    @Delete("DELETE FROM exam_wrong_question WHERE user_id = #{userId}")
    int deleteByUserId(@Param("userId") String userId);

    /**
     * 根据题目ID删除所有错题记录
     * 
     * @param questionId 题目ID
     * @return 删除行数
     */
    @Delete("DELETE FROM exam_wrong_question WHERE question_id = #{questionId}")
    int deleteByQuestionId(@Param("questionId") String questionId);

    /**
     * 获取高频错题（按错误次数排序）
     * 
     * @param userId 用户ID
     * @param limit 限制数量
     * @return 高频错题列表
     */
    @Select("SELECT * FROM exam_wrong_question WHERE user_id = #{userId} AND is_mastered = 0 " +
            "ORDER BY wrong_count DESC, last_wrong_time DESC LIMIT #{limit}")
    List<WrongQuestion> selectFrequentWrongQuestions(@Param("userId") String userId, @Param("limit") Integer limit);

    /**
     * 获取最近错误的题目
     * 
     * @param userId 用户ID
     * @param limit 限制数量
     * @return 最近错误的题目列表
     */
    @Select("SELECT * FROM exam_wrong_question WHERE user_id = #{userId} AND is_mastered = 0 " +
            "ORDER BY last_wrong_time DESC LIMIT #{limit}")
    List<WrongQuestion> selectRecentWrongQuestions(@Param("userId") String userId, @Param("limit") Integer limit);

    /**
     * 获取用户错题的题目ID列表
     * 
     * @param userId 用户ID
     * @param isMastered 是否已掌握
     * @return 题目ID列表
     */
    @Select("SELECT question_id FROM exam_wrong_question WHERE user_id = #{userId} AND is_mastered = #{isMastered}")
    List<String> selectQuestionIdsByUserId(@Param("userId") String userId, @Param("isMastered") Integer isMastered);
}
